/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.core.url;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map.Entry;

import org.apache.solr.common.util.SimpleOrderedMap;

/**
 * SolrDimension uses different parameter for filtering and prefixing.
 * 
 * QueryGen class is for translate the filter and prefix to a format solr can understand while retaining existing 
 * URL query params.
 * 
 * 
 * Assumes a valid Query
 * @author mhc
 *
 */
public class QueryGen {
    
    private static final String UTF8 ="UTF-8";
    
    protected static final String FILTER = "qf";
    
    protected static final String Q = "q";
    
    private static final String AND = "&";
    
    private static final String EQ = "=";
    
    SimpleOrderedMap<String> simpleOrderMap = new SimpleOrderedMap<String>();
    
    
    public QueryGen(String queryString){
        
        if (queryString != null && !queryString.isEmpty()){
            
            String[] queryStringArray = queryString.split(AND);
            
            if (queryStringArray.length > 0){
                
                for (String string : queryStringArray){
                    
                    String[] paramValue = string.split(EQ);
                    
                    if (paramValue.length == 2){
                        
                        simpleOrderMap.add(paramValue[0], paramValue[1]);
                        
                    }
                    
                }
                
            }
            
        }
        
    }
    
    public void addParam(String paramName, String value){
        
        if (Q.equals(paramName)){
            
            if (simpleOrderMap.get(paramName) != null){
                simpleOrderMap.remove(Q); 
            }
            
        }
        
        simpleOrderMap.add(paramName, value);
        
    }
    
    /**
     * remove ALL params with paramName
     * @param paramName
     */
    public void removeParam(String paramName){
        
        while (simpleOrderMap.get(paramName) != null){
            simpleOrderMap.remove(paramName);
        }
        
    }
    
    public String getFacetFilters(){
        
        StringBuffer result = null;
        
        List<String> filters = simpleOrderMap.getAll(FILTER);
        
        for (String filter : filters){
            
            //result.append(b)
            
        }
        
        return result.toString();
        
    }
    
    
    @Override
    public String toString(){
        
        StringBuffer result = new StringBuffer();
        
        
        for (Entry<String, String> entry : simpleOrderMap){
            
            result.append(entry.getKey());
            try {
                result.append(URLEncoder.encode(entry.getValue(), UTF8));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            
        }
        
        return result.toString();
        
    }

    
    
    
}
